﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TEL.TelPhoneF.InfoList
{
    public class Contact
    {
        public string ContactName { get; set; }
        public string ContactPhone { get; set; }
        public bool Favorite { get; set; }

        public virtual bool Check(ContactCallState state, string searchData)
        {
            bool name = true;
            bool phone = true;


            if (!string.IsNullOrEmpty(searchData))
            {
                name = FindByName(searchData);
                phone = FindByPhone(searchData);
            }
            return  (name || phone);
        }

        public bool FindByName ( string searchData)
        {
            bool res = true;
            if (!string.IsNullOrEmpty(searchData))
            {
                if (string.IsNullOrEmpty(ContactName)) res = false;
                else res = Regex.IsMatch(ContactName, string.Format("(^|\\s){0}", searchData), RegexOptions.IgnoreCase);
            }
            return res;
        }

        public bool FindByPhone(string searchData)
        {
            bool res = true;
            if (!string.IsNullOrEmpty(searchData))
            {
                if (string.IsNullOrEmpty(ContactPhone)) res = false;
                else res = Regex.IsMatch(ContactPhone, string.Format("^{0}", searchData), RegexOptions.IgnoreCase);
            }
            return res;
        }
    }
}
